<?php

namespace common\models\marathon;

use Yii;
use yii\helpers\ArrayHelper;
use common\models\base\BaseDist;

/**
 * This is the model class for table "{{%marathon_enroll_player}}".
 *
 * @property int $id
 * @property int $user_id
 * @property int $enroll_id 报名ID
 * @property int $match_id 比赛ID
 * @property int $item_id 比赛项目ID
 * @property string $mobile
 * @property string $email
 * @property string $realname
 * @property int $gender 性别：1男，2女，0未知
 * @property int $nationality 国籍：认默中国
 * @property int $nation 民族
 * @property int $ict_id 证件类型ID
 * @property string $idcardno 证件号码
 * @property string $birthday 出生日期
 * @property string $province
 * @property string $city
 * @property string $dist
 * @property string $address 通信地址
 * @property string $area_code
 * @property string $blood_type 血型
 * @property string $cloth_size 衣服尺码
 * @property string $education 教育程度学历
 * @property int $number_id 参赛号码
 * @property int $audit_status
 * @property int $status 参赛人状态
 * @property int $listorder
 * @property string $mark
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class MarathonEnrollPlayer extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    const STATUS_CANCEL = -1;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%marathon_enroll_player}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'enroll_id', 'match_id', 'item_id', 'gender', 'nationality', 'nation', 'ict_id', 'number_id', 'audit_status', 'status', 'listorder', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['mobile', 'email', 'realname', 'idcardno'], 'required'],
            [['birthday'], 'safe'],
            [['mobile'], 'string', 'max' => 11],
            [['email', 'realname', 'province', 'city', 'dist'], 'string', 'max' => 50],
            [['idcardno'], 'string', 'max' => 32],
            [['address'], 'string', 'max' => 100],
            [['area_code'], 'string', 'max' => 6],
            [['blood_type', 'cloth_size', 'education'], 'string', 'max' => 50],
            [['mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'enroll_id' => '报名ID',
            'match_id' => '比赛ID',
            'item_id' => '比赛项目ID',
            'mobile' => '手机号',
            'email' => 'Email',
            'realname' => '姓名',
            'gender' => '性别',
            'nationality' => '国籍',
            'nation' => '民族',
            'ict_id' => '证件类型ID',
            'idcardno' => '证件号码',
            'birthday' => '出生日期',
            'province' => 'Province',
            'city' => 'City',
            'dist' => 'Dist',
            'address' => '通信地址',
            'area_code' => 'Area Code',
            'blood_type' => '血型',
            'cloth_size' => '衣服尺码',
            'education' => '教育程度学历',
            'number_id' => '参赛号码',
            'audit_status' => 'Audit Status',
            'status' => '参赛人状态',
            'listorder' => 'Listorder',
            'mark' => 'Mark',
            'created_at' => '报名时间',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
            //
            'genderText' => '性别',
            'countryName' => '国家/地区',
            'addressText' => '省市区',
        ];
    }

    public function getEnroll() {
        return $this->hasOne(MarathonEnroll::className(), ['id' => 'enroll_id']);
    }

    public function getMatch() {
        return $this->hasOne(\common\models\match\Match::className(), ['id' => 'match_id']);
    }

    public function getMatchItem() {
        return $this->hasOne(\common\models\match\MatchItem::className(), ['id' => 'item_id']);
    }

    public function getLinkman() {
        return $this->hasOne(MarathonEnrollLinkman::className(), ['player_id' => 'id']);
    }

    public function getIct() {
        return $this->hasOne(\common\models\base\BaseIdcardType::className(), ['id' => 'ict_id']);
    }

    public function getCountry() {
        return $this->hasOne(\common\models\base\BaseCountry::className(), ['id' => 'nationality']);
    }

    public function getNations() {
        return $this->hasOne(\common\models\base\BaseNation::className(), ['id' => 'nation']);
    }

    public function isConfirm() {
        return $this->status == static::STATUS_ACTIVE;
    }

    public function getGrade() {
        return $this->hasOne(MarathonEnrollPlayerData::className(), ['player_id' => 'id']);
    }

    public function getNumber() {
        return $this->hasOne(MarathonNumber::className(), ['id' => 'number_id']);
    }

    public function getNumberText() {
        $number = $this->number;
        if (!$number) {
            return '****';
        }
        return $number->number;
    }

    public function getAreaText() {
        return implode('', $this->areaList);
    }

    public function getAreaList() {
        return [
            $this->province,
            $this->city,
            $this->dist,
        ];
    }

    public function getGenderText() {
        $genders = ['未填写', '男', '女'];
        return ArrayHelper::getValue($genders, $this->gender, '--');
    }

    public function getAddressText() {
        $dist = BaseDist::findAll([
            'code' => $this->getAreaList()
        ]);
        return implode('', ArrayHelper::getColumn($dist, 'name'));
    }

    public function getCountryName() {
        if ($this->nationality == 44) {
            return '中国';
        }
        $country = $this->country;
        if (!$country) {
            return '--';
        }
        return $country->en;
    }

}
